//
//  LGECGDetailScoreCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/8/20.
//

#import "LGECGDetailScoreCell.h"

@interface LGECGDetailScoreCell()
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**  模糊层 */
@property (nonatomic , strong)UIImageView *effectView;
/**   */
@property (nonatomic , strong)UILabel *scoreLabel;
/**   */
@property (nonatomic , strong)UIView *lineView1;
@property (nonatomic , strong)UILabel *statusLabel1;
@property (nonatomic , strong)UILabel *segmentLabel1;
/**   */
@property (nonatomic , strong)UIView *lineView2;
@property (nonatomic , strong)UILabel *statusLabel2;
@property (nonatomic , strong)UILabel *segmentLabel2;
/**   */
@property (nonatomic , strong)UIView *lineView3;
@property (nonatomic , strong)UILabel *statusLabel3;
@property (nonatomic , strong)UILabel *segmentLabel3;
/**   */
@property (nonatomic , strong)UIView *lineView4;
@property (nonatomic , strong)UILabel *statusLabel4;
@property (nonatomic , strong)UILabel *segmentLabel4;
/**   */
@property (nonatomic , strong)UIView *lineView5;
@property (nonatomic , strong)UILabel *statusLabel5;

/**   */
@property (nonatomic , strong)UIImageView *tipView;
/**   */
@property (nonatomic , strong)NSArray *itemArray;

@end

@implementation LGECGDetailScoreCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubView];
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    [super setDataDic:dataDic];
    self.titleLabel.text = LGNSString(dataDic[@"title"]);
    NSArray *itemArray = dataDic[@"items"];
    self.itemArray = itemArray;
    CGFloat itemW = (Screen_W-viewPix(56)-viewPix(8))/itemArray.count;
    [self.lineView1 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(itemW));
    }];
    self.lineView5.hidden = YES;
    self.statusLabel5.hidden = YES;
    for (NSInteger i=0; i<itemArray.count; i++) {
        NSDictionary *tempDic = itemArray[i];
        UILabel *statusLabel = [self viewWithTag:1000+i];
        UIView *lineView = [self viewWithTag:2000+i];
        statusLabel.hidden = NO;
        lineView.hidden = NO;
        statusLabel.text = tempDic[@"title"];
        statusLabel.textColor = [UIColor colorWithString:@"#282828"];
        lineView.backgroundColor = [UIColor colorWithString:tempDic[@"color"]];
    }
    
    NSArray *segments = dataDic[@"segments"];
    self.segmentLabel4.hidden = YES;
    for (NSInteger i=0; i<segments.count; i++) {
        UILabel *segmentLabel = [self viewWithTag:3000+i];
        segmentLabel.hidden = NO;
        segmentLabel.text = segments[i];
    }
}

-(void)setResultDic:(NSDictionary *)resultDic{
    _resultDic = resultDic;
    NSInteger index = [resultDic[@"index"] integerValue];
    if (index>=0) {
        self.showData = YES;
        self.tipView.hidden = NO;
        self.scoreLabel.text = LGNSString(resultDic[@"score"]);
        NSDictionary *tempDic = self.itemArray[index];
        self.scoreLabel.textColor = [UIColor colorWithString:tempDic[@"color"]];
        CGFloat minValue = [tempDic[@"minValue"] floatValue];
        CGFloat maxValue = [tempDic[@"maxValue"] floatValue];
        CGFloat length = maxValue-minValue;
        CGFloat itemW = (Screen_W-viewPix(56)-viewPix(8))/self.itemArray.count;
        CGFloat offset = itemW * (([resultDic[@"score"] floatValue]-minValue)/length);
        UIView *lineView = [self viewWithTag:2000+index];
        UILabel *statusLabel = [self viewWithTag:1000+index];
        statusLabel.textColor = [UIColor colorWithString:tempDic[@"color"]];
        [self.tipView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.lineView1.mas_bottom).offset(viewPix(4));
            make.centerX.equalTo(lineView.mas_left).offset(offset);
            make.width.equalTo(@(viewPix(20)));
            make.height.equalTo(@(viewPix(8)));
        }];
    }else{
        self.showData = NO;
        self.tipView.hidden = YES;
    }
}

-(void)setShowData:(BOOL)showData{
    _showData = showData;
    self.effectView.hidden = showData;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.scoreLabel];
    [self.contentView addSubview:self.statusLabel1];
    [self.contentView addSubview:self.statusLabel2];
    [self.contentView addSubview:self.statusLabel3];
    [self.contentView addSubview:self.statusLabel4];
    [self.contentView addSubview:self.statusLabel5];
    [self.contentView addSubview:self.lineView1];
    [self.contentView addSubview:self.lineView2];
    [self.contentView addSubview:self.lineView3];
    [self.contentView addSubview:self.lineView4];
    [self.contentView addSubview:self.lineView5];
    [self.contentView addSubview:self.segmentLabel1];
    [self.contentView addSubview:self.segmentLabel2];
    [self.contentView addSubview:self.segmentLabel3];
    [self.contentView addSubview:self.segmentLabel4];
    [self.contentView addSubview:self.tipView];
    [self.contentView addSubview:self.effectView];
    [self.contentView addSubview:self.lineView];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(20));
        make.left.equalTo(self.contentView).offset(viewPix(28));
    }];
    
    [self.scoreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.centerY.equalTo(self.titleLabel);
    }];
    
    [self.statusLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).offset(viewPix(12));
        make.centerX.equalTo(self.lineView1);
    }];
    
    [self.statusLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.statusLabel1);
        make.centerX.equalTo(self.lineView2);
    }];
    
    [self.statusLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.statusLabel1);
        make.centerX.equalTo(self.lineView3);
    }];
    
    [self.statusLabel4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.statusLabel1);
        make.centerX.equalTo(self.lineView4);
    }];
    
    [self.statusLabel5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.statusLabel1);
        make.centerX.equalTo(self.lineView5);
    }];
    
    
    [self.lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.statusLabel1.mas_bottom).offset(viewPix(5));
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.width.equalTo(@((Screen_W-viewPix(56)-viewPix(8))/5.0));
        make.height.equalTo(@(viewPix(4)));
    }];
    
    [self.lineView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lineView1);
        make.left.equalTo(self.lineView1.mas_right).offset(viewPix(2));
        make.width.height.equalTo(self.lineView1);
    }];
    
    [self.lineView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lineView1);
        make.left.equalTo(self.lineView2.mas_right).offset(viewPix(2));
        make.width.height.equalTo(self.lineView1);
    }];
    
    [self.lineView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lineView1);
        make.left.equalTo(self.lineView3.mas_right).offset(viewPix(2));
        make.width.height.equalTo(self.lineView1);
    }];
    
    [self.lineView5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.lineView1);
        make.left.equalTo(self.lineView4.mas_right).offset(viewPix(2));
        make.width.height.equalTo(self.lineView1);
    }];
    
    [self.segmentLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView1.mas_bottom).offset(viewPix(18));
        make.centerX.equalTo(self.lineView1.mas_right).offset(viewPix(1));
        make.bottom.equalTo(self.contentView).offset(-viewPix(20));
    }];
    
    [self.segmentLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.segmentLabel1);
        make.centerX.equalTo(self.lineView2.mas_right).offset(viewPix(1));
    }];
    
    [self.segmentLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.segmentLabel1);
        make.centerX.equalTo(self.lineView3.mas_right).offset(viewPix(1));
    }];
    
    [self.segmentLabel4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.segmentLabel1);
        make.centerX.equalTo(self.lineView4.mas_right).offset(viewPix(1));
    }];
    
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView1.mas_bottom).offset(viewPix(4));
        make.centerX.equalTo(self.lineView1.mas_left);
        make.width.equalTo(@(viewPix(20)));
        make.height.equalTo(@(viewPix(8)));
    }];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(viewPix(28));
        make.right.equalTo(self.contentView).offset(-viewPix(28));
        make.bottom.equalTo(self.contentView);
        make.height.equalTo(@(0.5));
    }];
    
    [self.effectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.titleLabel);
        make.right.equalTo(self.scoreLabel);
        make.bottom.equalTo(self.segmentLabel1);
    }];
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UIImageView *)effectView{
    if(!_effectView){
        _effectView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"心律-模糊背景"]];
        _effectView.hidden = YES;
    }
    return _effectView;
}

-(UILabel *)scoreLabel{
    if(!_scoreLabel){
        _scoreLabel = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentRight lines:1];
    }
    return _scoreLabel;
}

-(UILabel *)statusLabel1{
    if(!_statusLabel1){
        _statusLabel1 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _statusLabel1.tag = 1000;
    }
    return _statusLabel1;
}

-(UILabel *)statusLabel2{
    if(!_statusLabel2){
        _statusLabel2 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _statusLabel2.tag = 1001;
    }
    return _statusLabel2;
}

-(UILabel *)statusLabel3{
    if(!_statusLabel3){
        _statusLabel3 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _statusLabel3.tag = 1002;
    }
    return _statusLabel3;
}

-(UILabel *)statusLabel4{
    if(!_statusLabel4){
        _statusLabel4 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _statusLabel4.tag = 1003;
    }
    return _statusLabel4;
}

-(UILabel *)statusLabel5{
    if(!_statusLabel5){
        _statusLabel5 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFont(14) textAlignment:NSTextAlignmentCenter lines:1];
        _statusLabel5.tag = 1004;
    }
    return _statusLabel5;
}

-(UIView *)lineView1{
    if(!_lineView1){
        _lineView1 = [[UIView alloc]init];
        _lineView1.cornerRidus = viewPix(2);
        _lineView1.tag = 2000;
    }
    return _lineView1;
}

-(UIView *)lineView2{
    if(!_lineView2){
        _lineView2 = [[UIView alloc]init];
        _lineView2.cornerRidus = viewPix(2);
        _lineView2.tag = 2001;
    }
    return _lineView2;
}

-(UIView *)lineView3{
    if(!_lineView3){
        _lineView3 = [[UIView alloc]init];
        _lineView3.cornerRidus = viewPix(2);
        _lineView3.tag = 2002;
    }
    return _lineView3;
}

-(UIView *)lineView4{
    if(!_lineView4){
        _lineView4 = [[UIView alloc]init];
        _lineView4.cornerRidus = viewPix(2);
        _lineView4.tag = 2003;
    }
    return _lineView4;
}

-(UIView *)lineView5{
    if(!_lineView5){
        _lineView5 = [[UIView alloc]init];
        _lineView5.cornerRidus = viewPix(2);
        _lineView5.tag = 2004;
    }
    return _lineView5;
}

-(UILabel *)segmentLabel1{
    if(!_segmentLabel1){
        _segmentLabel1 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _segmentLabel1.tag = 3000;
    }
    return _segmentLabel1;
}

-(UILabel *)segmentLabel2{
    if(!_segmentLabel2){
        _segmentLabel2 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _segmentLabel2.tag = 3001;
    }
    return _segmentLabel2;
}

-(UILabel *)segmentLabel3{
    if(!_segmentLabel3){
        _segmentLabel3 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _segmentLabel3.tag = 3002;
    }
    return _segmentLabel3;
}

-(UILabel *)segmentLabel4{
    if(!_segmentLabel4){
        _segmentLabel4 = [UILabel lableWithText:@"" colorString:@"#282828" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _segmentLabel4.tag = 3003;
    }
    return _segmentLabel4;
}

-(UIImageView *)tipView{
    if(!_tipView){
        _tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"心律-箭头"]];
    }
    return _tipView;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc]init];
        _lineView.backgroundColor = [UIColor colorWithString:@"#E0E0E0"];
    }
    return _lineView;
}

@end
